﻿using Microsoft.Phone.Tasks;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Smile.Compatibility
{
    public static class ChooserExtensionMethods
    {
        public static Task<TTaskEventArgs> ShowAsync<TTaskEventArgs>(this ChooserBase<TTaskEventArgs> chooser)
            where TTaskEventArgs : TaskEventArgs
        {
            var taskCompletionSource = new TaskCompletionSource<TTaskEventArgs>();

            EventHandler<TTaskEventArgs> completed = null;
            completed = (source, e) =>
                {
                    chooser.Completed -= completed;
                    taskCompletionSource.SetResult(e);
                };

            chooser.Completed += completed;
            chooser.Show();

            return taskCompletionSource.Task;
        }
    }
}
